package com.gaojiawei.multiThread01_ThreadCreateMethod;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 线程创建方式三：继承Callable接口，重写call方法，配合futurtask使用
 */
public class l2_ThreadCreateMethod3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MythreadCallable mythreadCallable = new MythreadCallable();
        FutureTask futureTask = new FutureTask<>(mythreadCallable);
        Thread thread = new Thread(futureTask);

        thread.start();

        //这里会发生阻塞
        Object o = futureTask.get();
        System.out.println((Integer) o);


        for(int i =0;i< 1000;i++){
            System.out.println("main"+i);
        }

    }
}

class MythreadCallable implements Callable{

    @Override
    public Object call() throws Exception {
        int sum = 0;
        for(int i = 0;i<1000;i++){
            System.out.println("Mythread"+i);
            sum+=i;
        }
        return sum;

    }
}
